package io.renren.common.utils;

import lombok.extern.slf4j.Slf4j;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.net.*;

@Slf4j
public class Ke {

    public static final Integer port = 9001;
    public static final String host = "192.168.1.3";

    /**
     * 客户端   发送TCP协议请求
     * @param str
     */
    public static void connectServerTCP (String str) {
        Socket socket = null;
        try {
            socket = new Socket(host, port);
            //接受服务端发送的数据
            BufferedReader input = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            //向服务端发送数据
            PrintStream out = new PrintStream(socket.getOutputStream());
            System.out.println("ready send info to server!!");
            out.println(str);
            System.out.println("send info to server: "+ str);
            //发送后断掉连接
            out.close();
            input.close();
        } catch (Exception e) {
            log.error("客户端链接异常"+ e);
        } finally {
            if (socket != null) {
                try {
                    socket.close();
                } catch (Exception e) {
                    socket = null;
                    log.error("关闭异常"+ e);
                }
            }
        }
    }
}
